﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BUS
{
    public class CAddInput
    {
        public static void AddString(ref RichTextBox _rtbChen,string Key)
        {
            int _start = _rtbChen.SelectionStart;
            int _lenght = _rtbChen.SelectionLength;
            for(int i=_start-1;i>=0;i--)
            {
                if (_rtbChen.Text[i] == '>' || _rtbChen.Text[i] == ')')
                    break;
                if (_rtbChen.Text[i] == '<' || _rtbChen.Text[i] == '(')
                {
                    _start = i;
                    break;
                }
            }
            for (int i = _rtbChen.SelectionStart + _lenght; i < _rtbChen.Text.Length; i++)
            {
                if (_rtbChen.Text[i] == '<' || _rtbChen.Text[i] == '(')
                    break;
                if (_rtbChen.Text[i] == '>' || _rtbChen.Text[i] == ')')
                {
                    _lenght = i-_start+1;
                    break;
                }
            }

            if (_lenght > 0)
            {

                _rtbChen.Text = _rtbChen.Text.Insert(_start + _lenght, "</" + Key + ">");
                _rtbChen.Text = _rtbChen.Text.Insert(_start, "<" + Key + ">");
                _rtbChen.SelectionStart = _start;
                _rtbChen.SelectionLength = _lenght + Key.Length*2 + 5;
            }
            else
            { 
                _rtbChen.Text = _rtbChen.Text.Insert(_start, "(" + "CTRL" + ")");
                _rtbChen.SelectionStart = _start + Key.Length +2;
            }
        }

        public static void AddDelay(ref RichTextBox _rtbChen, int Delay)
        {
            int _start = _rtbChen.SelectionStart;
            int _lenght = _rtbChen.SelectionLength;
            for (int i = _start - 1; i >= 0; i--)
            {
                if (_rtbChen.Text[i] == '>' || _rtbChen.Text[i] == ')')
                    break;
                if (_rtbChen.Text[i] == '<' || _rtbChen.Text[i] == '(')
                {
                    _start = i;
                    break;
                }
            }
            for (int i = _rtbChen.SelectionStart + _lenght; i < _rtbChen.Text.Length; i++)
            {
                if (_rtbChen.Text[i] == '<' || _rtbChen.Text[i] == '(')
                    break;
                if (_rtbChen.Text[i] == '>' || _rtbChen.Text[i] == ')')
                {
                    _lenght = i - _start + 1;
                    break;
                }
            }

            if (_lenght > 0)
            {

                _rtbChen.Text = _rtbChen.Text.Insert(_start + _lenght, "</Delay>");
                _rtbChen.Text = _rtbChen.Text.Insert(_start, "<Delay=" + Delay.ToString() + ">");
                _rtbChen.SelectionStart = _start;
                _rtbChen.SelectionLength = _lenght + Delay.ToString().Length + 16;
            }
            else
            {
                _rtbChen.Text = _rtbChen.Text.Insert(_start, "(Delay=" + Delay.ToString() + ")");
                _rtbChen.SelectionStart = _start + Delay.ToString().Length + 8;
            }
        }
    }
}
